<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="public/css/pages.css" type="text/css" rel="stylesheet">
<?php
require_once 'model/dbs.php';
?>

<h1>Bích Vân.</h1>
<?php
$query = "SELECT * FROM companies";
$companies = getData($query);
?>
<?php
$query = "SELECT * FROM apps";
$apps = getData($query);
?>
<form method="POST">
    Min fans :<input type="text" name="min_fans"><br>
    Max fans :<input type="text" name="max_fans"><br>
    Keyword :<input type="text" name="keyword"><br>
    Company :<select name="id_company">
<?php foreach ($companies as $company): ?>
            <option value="<?php echo $company['id'] ?>"><?php echo $company['name'] ?></option>
        <?php endforeach; ?>
    </select>
    Apps :<select name="id_apps">
<?php foreach ($apps as $app): ?>
            <option value="<?php echo $app['pid'] ?>"><?php echo $app['name'] ?></option>
        <?php endforeach; ?>
    </select><br>
    <input type="submit" name="submit">
</form>

<?php if (isset($_POST['submit'])): // isset kiem tra co ton tai kg?>
    <?php
    $min_fans = $_POST['min_fans']; 
    $max_fans = $_POST['max_fans']; 
    $keyword = $_POST['keyword']; 
    $company = $_POST["id_company"];
     $app = $_POST["id_apps"];
    if (empty($max_fans) && !empty($min_fans))  { //empty ktra rong kg.
        $query = "SELECT * FROM pages 
                  
                  WHERE
                      (likes >= $min_fans)
                      AND (description LIKE '% $keyword %')
                  LIMIT 0, 10
                  ";
    } else if (!empty($max_fans) && empty($min_fans)) {
        $query = "SELECT * FROM pages 
                  
                  WHERE
                      (likes <= $max_fans)
                      AND (description LIKE '% $keyword %')
                      
                  LIMIT 0, 10
                  ";
    } else if (empty($max_fans) && empty($min_fans)) {
        $query = "SELECT * FROM pages
             WHERE (description LIKE '% $keyword %')
                  LIMIT 0, 10
                  ";
    } else {
        $query = "SELECT * FROM pages    
                  WHERE
                      (likes >= $min_fans)
                      AND (likes <= $max_fans)
                      AND (description LIKE '% $keyword %')
                  LIMIT 0, 10
                  ";
    }

    $pages = getData($query);
    ?>

    <?php if (!empty($pages)): ?>
        <table id="khung">
            <tr >
                <td id="o"> profile </td>
                <td id="o"> likes </td>
                <td id="o"> des </td>
            </tr>
        <?php foreach ($pages as $page): ?>
            <tr class="a">
                <td ><img src="<?php echo $page['picture'] ?>"></td>
                <td class="b"><?php echo $page['likes'] ?></td>
                <td ><?php echo substr($page['description'],0,100) ?></td>
                </tr>
        <?php endforeach; ?>
        </table> 

    <?php endif; ?>
<?php endif; ?>
